package com.panghl.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.panghl.mapper.UserMapper;
import com.panghl.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class StatService {

    @Autowired
    private UserMapper userMapper;

    public List<Map> columnCharts() {
        return userMapper.columnCharts();
    }

    public List<Map> lineCharts() {
        return userMapper.lineCharts();
    }

    public List<Map> pieCharts() {
        List<Map> resultMapList = new ArrayList();
//        最终想要的数据格式：[{id:"河北省","drilldown":"河北省","name":"河北省","y":10,"data":[{"name":"石家庄","y":4},{"name":"唐山","y":3},{"name":"保定","y":3}]}]
        List<User> userList = userMapper.selectList(null); //查询所有员工数据
//        根据省份分组
        Map<String, List<User>> provinceMap = userList.stream().collect(Collectors.groupingBy(User::getProvince));
        for (String provinceName : provinceMap.keySet()) {
            int provinceSize = 0;
            Map resultMap = new HashMap();
            resultMap.put("id",provinceName);
            resultMap.put("drilldown",provinceName);
            resultMap.put("name",provinceName);
//            再根据城市分组
            Map<String, List<User>> cityMap = provinceMap.get(provinceName).stream().collect(Collectors.groupingBy(User::getCity));
            List<Map> dataMapList = new ArrayList<>();
            for (String cityName : cityMap.keySet()) {
                Map dataMap = new HashMap();
                dataMap.put("name",cityName);
                int citySize = cityMap.get(cityName).size();
                dataMap.put("y",citySize);
                dataMapList.add(dataMap);
                provinceSize+=citySize;
            }
            resultMap.put("y",provinceSize);
            resultMap.put("data",dataMapList);
            resultMapList.add(resultMap);
        }
        return resultMapList;
    }

    public  Map pieECharts() {
        Map resultMap = new HashMap();
        //        最终想要的数据格式：{province:[{name:,value:}],city:[]}
        List<User> userList = userMapper.selectList(new QueryWrapper<User>().lambda().orderByDesc(User::getProvince,User::getCity)); //查询所有员工数据
        //        根据省份分组
        List<Map> provinceMapList = new ArrayList<>();
        //        注意分组时得排序，不然数据会乱
        Map<String, List<User>> provinceMap = userList.stream().collect(Collectors.groupingBy(User::getProvince,LinkedHashMap::new,Collectors.toList()));
        for (String provinceName : provinceMap.keySet()) {
            Map map = new HashMap();
            map.put("name",provinceName);
            map.put("value",provinceMap.get(provinceName).size());
            provinceMapList.add(map);
        }

        List<Map> cityMapList = new ArrayList<>();
        //        注意分组时得排序，不然数据会乱
        Map<String, List<User>> cityMap = userList.stream().collect(Collectors.groupingBy(User::getCity, LinkedHashMap::new,Collectors.toList()));
        for (String cityName : cityMap.keySet()) {
            Map map = new HashMap();
            map.put("name",cityName);
            map.put("value",cityMap.get(cityName).size());
            cityMapList.add(map);
        }

        resultMap.put("province",provinceMapList);
        resultMap.put("city",cityMapList);

        return resultMap;
    }
}
